1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.solr.internal.csv;
18
19 import java.io.StringWriter;
20 import java.io.StringReader;
21 import java.io.IOException;
22
23 /**
24 * Utility methods for dealing with CSV files
25 */
26 public class CSVUtils {
27
28 private static final String[] EMPTY_STRING_ARRAY = new String[0];
29 private static final String[][] EMPTY_DOUBLE_STRING_ARRAY = new String[0][0];
30
31 /**
32 * <p><code>CSVUtils</code> instances should NOT be constructed in
33 * standard programming.
34 *
35 * <p>This constructor is public to permit tools that require a JavaBean
36 * instance to operate.</p>
37 */
38 public CSVUtils() {
39 }
40
41 /**
42 * Converts an array of string values into a single CSV line. All
43 * <code>null</code> values are converted to the string <code>"null"</code>,
44 * all strings equal to <code>"null"</code> will additionally get quotes
45 * around.
46 *
47 * @param values the value array
48 * @return the CSV string, will be an empty string if the length of the
49 * value array is 0
50 */
51 public static String printLine(String[] values, CSVStrategy strategy) {
52 // set up a CSVUtils
53 StringWriter stringWriter = new StringWriter();
54 CSVPrinter csvPrinter = new CSVPrinter(stringWriter, strategy);
55
56 // check for null values an "null" as strings and convert them
57 // into the strings "null" and "\"null\""
58 for (int i = 0; i < values.length; i++) {
59 if (values[i] == null) {
60 values[i] = "null";
61 } else if (values[i].equals("null")) {
62 values[i] = "\"null\"";
63 }
64 }
65
66 // convert to CSV
67 try {
68 csvPrinter.println(values);
69 } catch (IOException e) {
70 // should not happen with StringWriter
71 }
72 // as the resulting string has \r\n at the end, we will trim that away
73 return stringWriter.toString().trim();
74 }
75
76 // ======================================================
77 // static parsers
78 // ======================================================
79
80 /**
81 * Parses the given String according to the default {@link CSVStrategy}.
82 *
83 * @param s CSV String to be parsed.
84 * @return parsed String matrix (which is never null)
85 * @throws IOException in case of error
86 */
87 public static String[][] parse(String s) throws IOException {
88 if (s == null) {
89 throw new IllegalArgumentException("Null argument not allowed.");
90 }
91 String[][] result = (new CSVParser(new StringReader(s))).getAllValues();
92 if (result == null) {
93 // since CSVStrategy ignores empty lines an empty array is returned
94 // (i.e. not "result = new String[][] {{""}};")
95 result = EMPTY_DOUBLE_STRING_ARRAY;
96 }
97 return result;
98 }
99
100 /**
101 * Parses the first line only according to the default {@link CSVStrategy}.
102 *
103 * Parsing empty string will be handled as valid records containing zero
104 * elements, so the following property holds: parseLine("").length == 0.
105 *
106 * @param s CSV String to be parsed.
107 * @return parsed String vector (which is never null)
108 * @throws IOException in case of error
109 */
110 public static String[] parseLine(String s) throws IOException {
111 if (s == null) {
112 throw new IllegalArgumentException("Null argument not allowed.");
113 }
114 // uh,jh: make sure that parseLine("").length == 0
115 if (s.length() == 0) {
116 return EMPTY_STRING_ARRAY;
117 }
118 return (new CSVParser(new StringReader(s))).getLine();
119 }
120
121 }